#include <Ipc.h>
#include <XmlParser.h>
#include "netAlarm.h"

extern struct devconfig *global_config;
extern int msgid, shmid_config;
extern int alarmFd[200];

void GetAlarmConfInfo(int fd, char *buff, int blen)
{
	char *buffer = buff + LOCAL_HDR_LEN;
	int len = blen-LOCAL_HDR_LEN;

	if(len > MAX_MSG_LEN)
	{
		LOGERROR("ProbeInfo length is greater than 512!\n");
		return;
	}
    SendIPCMsg(buffer, len, MSG_ALARM_CONF);
#if 0
	int msglen = 0;
	unsigned char msgbuf[MAX_MSG_LEN];
	handleAlarmMsg(msgbuf, &msglen);
	SendAlarmMsg(fd, msgbuf, msglen);
#endif
	return;
}

void GetAlarmMsg(int fd, char *buffer, int blen)
{
    //unsigned char msg_to_send[10]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
	char *buff = buffer+LOCAL_HDR_LEN;
	int len = blen-LOCAL_HDR_LEN;

	if(0x00 == *buff)
	{
		int i = 0, num = 0;
	    while(i < 200)
		{
			if(alarmFd[i] > 0)
			    num++;
			i++;
		}
		LOGINFO("Num   :   %d\n", num);
		if(1 != num)
			return;
	}
	SendIPCMsg(buff, len, MSG_ALARM_LISTEN);
	//handleAlarmMsg(sockfd);
#if 0
	SendAlarmMsg(sockfd, msgbuf, msglen);
#endif
	return;
}

void SendIPCMsg(char *buff, int blen, int type)
{
    /* add cmd to msg buffer */
    if(kmsg_add(msgid, MSGID_NET,MSGID_ALMHANDLER,
                /*MSG_ALARM_CONF*/type,
                buff, blen) < 0)
		return ;

	LOGINFO("[Net]:send msg!\n");
	return;
}

#if 0
void handleAlarmMsg(int fd)
{
	int msglen = 0, sockfd = fd, num = 0;
	unsigned char msgbuf[MAX_MSG_LEN] = {0};
	struct timeval tv;



	/* read message */
	while (1) {
		char buffer[MAX_MSG_LEN];

		if (kmsg_read(msgid, MSGID_NET, msgbuf, &msglen))
		{
			struct cmdunitl2 *cmdlist = NULL, *node = NULL;
			int src = 0, seq = 0, cmdnum = 0;
			/* get cmd units */
			cmdnum = msg2cmdl2(msgbuf, msglen, &cmdlist, &src, &seq);
			node = cmdlist;
			while(NULL != node)
			{
				switch(node->type)
				{
					case MSG_ALARM_MSG:
						LOGINFO("MSG_ALARM_LISTEN Message!\n");
						SendAlarmMsg(sockfd, node->cmd, node->len);
						break;
					default:
						LOGINFO("MSG TYPE ERROR!\n");
						break;
				}
				node = node->next;
			}
			cmdfree(cmdlist);
		}
	}
	return;
}
#endif

void SendAlarmMsg(int fd, unsigned char *msgbuf, int msglen)
{
	char *buffer = NULL;
	int sockfd = fd;
	int len = msglen, hdrlen = LOCAL_HDR_LEN;

	len += hdrlen;
	buffer = (char *)malloc(len*sizeof(char));
	memset(buffer, 0 ,len);
#if 0
	buffer[0] = 0xFF;
	buffer[1] = 0xFF;
	buffer[2] = 0xFF;
	buffer[3] = 0xFF;
	buffer[5] = *(char *)&len;
	buffer[6] = *((char *)&len + 1);
	buffer[31] = 0x04;
	buffer[32] = 0x03;
#else
	int code = 0x0403;
	InitLocalProHdr(buffer, code, len);
#endif
	memcpy(buffer + hdrlen, msgbuf, msglen);
    if(SendDataFunc(buffer, len, len, NULL, sockfd) == -1)
    {
        LOGERROR("SendAlarmMsg : SendDataFunc Error!\n");
	}

	free(buffer);
	return;
}

void GetAlarmOutputState(int fd, char *buff, int blen)
{
	int sockfd = fd, size = 0, len = 0;
	char statebuf[17] = {0}, flag = 0, *msg = NULL;
	FILE *fp = OPEN_FILE("../config/get_alarmoutstate.xml", "wb");
    VSP_RECVHEADER hdr;
	//get device id

    fwrite(XMLHEADER, 1, strlen(XMLHEADER), fp);
	fwrite(START_MESSAGE, 1, strlen(START_MESSAGE), fp);
	sprintf(statebuf, "<State>%d</State>\n", flag);
	fwrite(statebuf, 1, strlen(statebuf), fp);
	fwrite(END_MESSAGE, 1, strlen(END_MESSAGE), fp);

	size = ftell(fp);
	fclose(fp);
	fp = OPEN_FILE("../config/get_alarmoutstate.xml", "rb");
	hdr = InitVSP_RECVHeader(size, GET_ALARM_OUTPUT_STATE_RES, 0, 0);
	len = size + sizeof(hdr);
    msg = (char *)malloc(len*sizeof(char));
    memcpy(msg, &hdr, sizeof(hdr));
    if(SendDataFunc(msg, len, sizeof(hdr), fp, sockfd) == -1)
    {
	    LOGERROR("SendDataFunc error!\n");
    }
	fclose(fp);
	free(msg);

	return;
}

void SetAlarmOutputState(int fd, char *buff, int blen)
{
	char statebuf[50] = {0}, *msg = NULL, restr[] = {"Succ!"};
	FILE *fp = OPEN_FILE("../config/set_alarmoutstate.xml", "wb");
    VSP_RECVHEADER hdr;
	int sockfd = fd, size = 0, len = 0;

	//get device id and op
	//SendIPCMsg(buff, blen, MSG_ALARM_CONF);

	fwrite(START_MESSAGE, 1, strlen(START_MESSAGE), fp);
	sprintf(statebuf, "<ErrScription>%s</ErrScription>\n", restr);
	fwrite(statebuf, 1, strlen(statebuf), fp);
	fwrite(END_MESSAGE, 1, strlen(END_MESSAGE), fp);
	size = ftell(fp);
	fclose(fp);
	fp = OPEN_FILE("../config/set_alarmoutstate.xml", "rb");
	hdr = InitVSP_RECVHeader(size, SET_ALARM_OUTPUT_STATE_RES, 0 ,0);
	len = size + sizeof(hdr);
    msg = (char *)malloc(len*sizeof(char));
    memcpy(msg, &hdr, sizeof(hdr));
    if(SendDataFunc(msg, len, sizeof(hdr), fp, sockfd) == -1)
    {
	    LOGERROR("SendDataFunc error!\n");
    }
	fclose(fp);
	free(msg);

	return;
}

void SetAlarmPlan(int fd, char *buff, int blen)
{
	int sockfd = fd, len = blen;
	char *msg = NULL;
    VSP_RECVHEADER hdr;

	//SendIPCMsg(buff, blen, MSG_ALARM_CONF);

	hdr = InitVSP_RECVHeader(0, SET_ALARM_PLAN_RES, 0 ,0);
	len = sizeof(hdr);
    msg = (char *)malloc(len*sizeof(char));
    memcpy(msg, &hdr, sizeof(hdr));
    if(SendDataFunc(msg, sizeof(hdr), sizeof(hdr), NULL, sockfd) == -1)
    {
	    LOGERROR("SendDataFunc error!\n");
    }
	free(msg);

	return;
}

void GetAlarmPlan(int fd, char *buff, int blen)
{
	int sockfd = fd, len = 0, size = 0;
	char *plan = NULL, *planTime = NULL, *msg = NULL;
    VSP_RECVHEADER hdr;
	FILE *fp = OPEN_FILE("../config/get_alarm_plan.xml", "wb");
    // VSP_RECVHEADER hdr;

	planTime = (char *)malloc(168 *sizeof(char));
	plan = (char *)malloc(184 *sizeof(char));
	memset(planTime, '1', 168);
	planTime[168] = '\0';
	fwrite(START_MESSAGE, 1, strlen(START_MESSAGE), fp);
	sprintf(plan, "<Plan>%s</Plan>\n", planTime);
	free(planTime);
	fwrite(plan, 1, strlen(plan), fp);
	free(plan);
	fwrite(END_MESSAGE, 1, strlen(END_MESSAGE), fp);
	size = ftell(fp);
	fclose(fp);
	fp = OPEN_FILE("../config/get_alarm_plan.xml", "rb");
	hdr = InitVSP_RECVHeader(size, GET_ALARM_PLAN_RES, 0 ,0);
	len = size + sizeof(hdr);
    msg = (char *)malloc(len*sizeof(char));
    memcpy(msg, &hdr, sizeof(hdr));
    if(SendDataFunc(msg, len, sizeof(hdr), fp, sockfd) == -1)
    {
	    LOGERROR("SendDataFunc error!\n");
    }
	fclose(fp);
	free(msg);
#if 0
	len = sizeof(hdr) + strlen(START_MESSAGE) + strlen("<Plan>") + 168 +
		  strlen("</Plan>") + strlen(END_MESSAGE);
	char *buffer = (char *)malloc(len * sizeof(char));
	char *pos = buffer;
	hdr = InitVSP_RECVHeader(len, GET_ALARM_PLAN_RES, 0 ,0);
    memcpy(pos, &hdr, sizeof(hdr));
	planTime = (char *)malloc(168 *sizeof(char));
	memset(planTime, '1', 168);
	planTime[168] = '\0';
	pos += sizeof(hdr);
	sprintf(pos,"%s\n", START_MESSAGE);
	sprintf(pos+strlen(START_MESSAGE), "<Plan>%s</Plan>\n", planTime);
	free(planTime);
	pos += strlen("<Plan>") + 168 + strlen("</Plan>");
	sprintf(pos,"%s\n", END_MESSAGE);
    if(SendDataFunc(buffer, len, len, NULL, sockfd) == -1)
    {
	    LOGERROR("SendDataFunc error!\n");
    }
	free(buffer);
#endif
	return;
}


void RecvAlarmNotice(int fd, char *buff, int blen)
{
	int sockfd = fd, len = blen, size = 0;
	char *msg = NULL, *eventNaming = NULL, *eventTypeID = NULL,
		 *eventTypeName = NULL, eventbuf[200] = {0};
	mxml_node_t *root = NULL, *node = NULL;
	FILE *fp = OPEN_FILE("../config/recv_alarm_notice.xml", "wb");
    VSP_RECVHEADER hdr;

	//SendIPCMsg(buff, blen, MSG_ALARM_CONF);

	root = (mxml_node_t *)ParserXML(buff, len, VSP_SEND_XML);
	node = (mxml_node_t *)HaveNode(root, "Event");
	eventNaming = GetKeyValue(node, "EventNaming");
	eventTypeID = GetKeyValue(node, "EventTypeID");
	eventTypeName = GetKeyValue(node, "EventTypeName");

	fwrite(START_MESSAGE, 1, strlen(START_MESSAGE), fp);
	sprintf(eventbuf, "<Event EventNaming=\"%s\" EventTypeID=\"%s\"\
		   EventTypeName=\"%s\" \\>\n", eventNaming, eventTypeID, eventTypeName);
	fwrite(eventbuf, 1, strlen(eventbuf), fp);
	fwrite(END_MESSAGE, 1, strlen(END_MESSAGE), fp);
	size = ftell(fp);
	fseek(fp, 0L, SEEK_SET);
	len = size + sizeof(hdr);
	hdr = InitVSP_RECVHeader(size, ALARM_NOTICE_MSG_RES, 0 ,0);
    msg = (char *)malloc(len*sizeof(char));
    memcpy(msg, &hdr, sizeof(hdr));
    if(SendDataFunc(msg, len, sizeof(hdr), fp, sockfd) == -1)
    {
	    LOGERROR("SendDataFunc error!\n");
    }

	return;
}


void FetchLocalAlarmConf(int sockfd, int code)
{
	char *buffer = NULL;
	int len = SENSOR_BUFF_SIZE;
	int i;

	kshm_lock(shmid_config);
	for(i=0;i < SENSOR_NUM; i++)
	{
		buffer = (char *)malloc((LOCAL_HDR_LEN+len) * sizeof(char));
		memset(buffer, 0, LOCAL_HDR_LEN+len);
		memcpy(buffer+LOCAL_HDR_LEN, &global_config->alarmConf.aaSensor[i][0], len);
		InitLocalProHdr(buffer, code, LOCAL_HDR_LEN+len);
		if(SendDataFunc(buffer, LOCAL_HDR_LEN+len, LOCAL_HDR_LEN+len, NULL, sockfd) == -1)
		{
			LOGERROR("SendDataFunc error!\n");
		}
		free(buffer);
	}

	kshm_unlock(shmid_config);
    	return;

}
